package me.zhengjie.service.impl;

import cn.hutool.http.Header;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.exception.BadRequestException;
import me.zhengjie.service.vivoService;
import org.springframework.stereotype.Service;

import java.net.InetSocketAddress;
import java.net.Proxy;
import java.util.HashMap;
import java.util.Map;

@Service
@RequiredArgsConstructor
public class vivoServiceImpl implements vivoService {

    @Override
    public String getCode(String phone) {
        HttpResponse response = HttpRequest.post("https://passport.vivo.com.cn/v5/smsLogin/p1")
                        .header(Header.HOST,"passport.vivo.com.cn")
                        //.header("sec-ch-ua","\" Not A;Brand\";v=\"99\", \"Chromium\";v=\"90\", \"Google Chrome\";v=\"90\"")
                        .header("Accept","application/json, text/plain, */*")
                        .header("Referer","https://passport.vivo.com.cn/")
                        .header("Cookie","vivo_account_cookie_iqoo_deviceid=wb_52985d22-817d-497f-a86f-85db9c52c4d9; language=zh_CN; JSESSIONID=40424B02263FF01AE538EF4D47A7C773; log_r_reuslt=NORISK; log_r_flow_no=PASSPORT20210608175658638")
                        //.header("sec-ch-ua-mobile","?0")
                        .header("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36")
                        .form(initFromData(phone))
                        .timeout(20000).setProxy(new Proxy(java.net.Proxy.Type.HTTP,new InetSocketAddress("127.0.0.1", 8888)))
                        .execute();
        System.out.println(response.toString());
        if(200!=response.getStatus()){
            throw new BadRequestException("获取验证码失败");
        }
        JSONObject resJson = JSONUtil.parseObj(response.body());
        String  code  = String.valueOf(resJson.get("code"));
        if(!"0".equals(code)){
            switch (code)
            {
                case "10110":throw new BadRequestException("请换账号密码登录");
                default :throw new BadRequestException(String.valueOf(resJson.get("msg")));

            }
        }
        return null;
    }

    @Override
    public Boolean phoneLogin(String phone, String code) {
        return null;
    }

    private Map<String,Object> initFromData(String phone){
        HashMap<String,Object> paraMap = new HashMap<>();
        paraMap.put("areaCode","86");
        paraMap.put("ticket","");
        paraMap.put("randomNum","");
        paraMap.put("client_id","9");
        paraMap.put("sliderVersionType","2");
        paraMap.put("clientType","2");
        paraMap.put("countryCode","CN");
        paraMap.put("deviceType","pc");
        paraMap.put("nounce","6a3344b44304fab5db937b5017734a4c");
        paraMap.put("locale","zh_CN");
        paraMap.put("authcookie","1");
        paraMap.put("phone",phone);
        paraMap.put("timeStamp",String.valueOf(System.currentTimeMillis()));
        return  paraMap;
    }

    public static void main(String[] args) {
        vivoServiceImpl a = new vivoServiceImpl();
        a.getCode("13260653717");
    }
}
